package terra.model.object;

import terra.Config;
import terra.network.packet.server.ObjectMove;

public abstract class MovableObject extends GameObject {

	private static final long serialVersionUID = -2065872773206072083L;

	static final byte MOVE_ON_GROUND = 0;
	static final byte MOVE_ON_WATER = 1;
	static final byte MOVE_UNDER_WATER = 2;
	static final byte MOVE_NONE = 3;
	static final byte MOVE_ON_LADDER = 4;
	private float speed = 128f;
	private float jumpStartSpeed = 6f; // 6f
	private long jumpTimer = System.currentTimeMillis();
	private long jumpTime = 750L;
	private boolean allowJump = true;
	// угол поворота головы объекта
	private float heading = 0, oldHeading = 0;

	public float getHeading() {
		return heading;
	}

	public void setHeading(float heading) {
		oldHeading = this.heading;
		this.heading = heading;
	}

	public boolean positionChanged() {
		if (Math.abs(Math.round(oldHeading - getHeading())) != 0)
			return true;
		return super.positionChanged();
	}

	public MovableObject(int objectId) {
		super(objectId);
	}

	protected void updateGravity(int delta) {
		float newY = getY();
		if (!isCollision2(getX(),
				(getY() + getYacceleration() + (Config.gravity / 1000) * delta))) {
			setYacceleration(getYacceleration() + (Config.gravity / 1000)
					* delta);
			if (getYacceleration() > Config.maxGravity)
				setYacceleration(Config.maxGravity);
			newY = getY() + getYacceleration();
		} else
			setYacceleration(0);
		if (!isCollision2(getX(), newY))
			setY(newY);

	}

	public long getJumpTimer() {
		return jumpTimer;
	}

	public void setJumpTimer(long jumpTimer) {
		this.jumpTimer = jumpTimer;
	}

	public long getJumpTime() {
		return jumpTime;
	}

	public void setJumpTime(long jumpTime) {
		this.jumpTime = jumpTime;
	}

	public float getJumpStartSpeed() {
		return jumpStartSpeed;
	}

	public void setJumpStartSpeed(float jumpStartSpeed) {
		this.jumpStartSpeed = jumpStartSpeed;
	}

	public float getSpeed() {
		return speed;
	}

	public void setSpeed(float speedPerSecond) {
		this.speed = speedPerSecond;
	}

	public boolean isAllowJump() {
		return allowJump;
	}

	public void setAllowJump(boolean allowJump) {
		this.allowJump = allowJump;
	}

	@Override
	public ObjectMove getObjectMove() {
		om.oId = getObjectId();
		om.x = getX();
		om.y = getY();
		om.heading = heading;
		om.num = getObjectMoveNum();
		om.left = this.isLeft();
		return om;
	}
}
